﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using WeifenLuo.WinFormsUI.Docking;

namespace WL_WinForm
{
    public partial class Admin : DockContent
    {
        public Admin()
        {
            InitializeComponent();
            Init();
            BindData();
        }

        private void Init()
        {
            DataTable dt = BLL.wlBLL.BllComboList.GetPurview();
            COMMON.ControlCommon.ComboBinds(COMMON.ControlCommon.ConvertToComboList(dt, "PurviewGrade", "PurviewID"), cbPurviewID);
        }

        private void BindData()
        {
            DataTable dt = new BLL.wlBLL.User().GetUser();
            gvUser.DataSource = dt;
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            if (txtUserName.Text == "")
            {
                COMMON.ControlCommon.ShowMessageBoxError("请输入用户名！");
                return;
            }
            if (cbPurviewID.SelectedValue.ToString() == "-999")
            {
                COMMON.ControlCommon.ShowMessageBoxError("请选择权限！");
                return;
            }

            WLong.BLL.Rel_User user = new WLong.BLL.Rel_User();

            if (txtUserID.Text != "")
            {
                WLong.Model.Rel_User mUser = new WLong.Model.Rel_User();
                mUser.UserID = new Guid(txtUserID.Text);
                mUser.UserName = txtUserName.Text.Trim();
                mUser.UserPassWord = COMMON.MD5.Encrypt(txtUserPassWord.Text.Trim(), COMMON.MD5.md5Key);
                mUser.PurviewID = new Guid (cbPurviewID.SelectedValue.ToString());
                mUser.Disabled = 0;
                mUser.UserState = ckUserState.Checked ? 1 : 0;
                mUser.REC_ModifyBy = COMMON.Login.loginName;
                mUser.REC_ModifyTime = DateTime.Now;

                if (user.Update(mUser))
                {
                    ClearControl();
                    BindData();
                    COMMON.ControlCommon.ShowMessageBoxOk("修改成功！");
                }
                else
                {
                    COMMON.ControlCommon.ShowMessageBoxError("修改失败！");
                }
            }
            else
            {
                WLong.Model.Rel_User mUser = new WLong.Model.Rel_User();
                mUser.UserID = Guid.NewGuid();
                mUser.UserName = txtUserName.Text.Trim();
                mUser.UserPassWord = COMMON.MD5.Encrypt(txtUserPassWord.Text.Trim(),COMMON.MD5.md5Key);
                mUser.PurviewID = new Guid(cbPurviewID.SelectedValue.ToString());
                mUser.Disabled = 0;
                mUser.UserState = ckUserState.Checked ? 1 : 0;
                mUser.REC_CreateBy = COMMON.Login.loginName;
                mUser.REC_CreateTime = DateTime.Now;

                user.Add(mUser);
                ClearControl();
                BindData();
            }
        }

        private void ClearControl()
        {
            txtUserID.Text = "";
            txtUserName.Text = "";
            txtUserPassWord.Text = "";
            cbPurviewID.SelectedValue = "-999";
            ckUserState.Checked = false;
        }

        private void gvUser_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            txtUserID.Text = gvUser.CurrentRow.Cells["UserID"].Value.ToString();
            txtUserName.Text = gvUser.CurrentRow.Cells["UserName"].Value.ToString();
            txtUserPassWord.Text = gvUser.CurrentRow.Cells["UserPassWord"].Value.ToString();
            if (gvUser.CurrentRow.Cells["UserState"].Value.ToString() == "1")
            {
                ckUserState.Checked =true;
            }
            else
	        {
                ckUserState.Checked = false;
	        }
            cbPurviewID.SelectedValue = gvUser.CurrentRow.Cells["PurviewID"].Value.ToString();
        }

        private void gvUser_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Delete)
            {
                WLong.BLL.Rel_User user = new WLong.BLL.Rel_User();
                WLong.Model.Rel_User mUser = new WLong.Model.Rel_User();
                mUser.UserID = new Guid(gvUser.CurrentRow.Cells["UserID"].Value.ToString());
                mUser.UserName = gvUser.CurrentRow.Cells["UserName"].Value.ToString();
                mUser.UserPassWord = gvUser.CurrentRow.Cells["UserPassWord"].Value.ToString();
                mUser.PurviewID = new Guid(gvUser.CurrentRow.Cells["PurviewID"].Value.ToString());
                mUser.Disabled = 1;
                mUser.UserState = int.Parse(gvUser.CurrentRow.Cells["UserState"].Value.ToString());
                mUser.REC_ModifyBy = COMMON.Login.loginName;
                mUser.REC_ModifyTime = DateTime.Now;
                if (user.Update(mUser))
                {
                    BindData();
                    COMMON.ControlCommon.ShowMessageBoxOk("删除成功！");
                }
                else
                {
                    COMMON.ControlCommon.ShowMessageBoxError("删除失败！");
                }
            }
        }
    }
}
